import 'dart:developer';

import 'package:bloc/bloc.dart';

/// 事件
enum NumberEvent { addEvent }

/// 数字更新Bloc
class NumberBloc extends Bloc<NumberEvent, int> {
  NumberBloc(int initialState) : super(initialState);

  // 默认是10
  @override
  int get initialState => 10;

  @override
  Stream<int> mapEventToState(NumberEvent event) async* {
    log("NumberBloc mapEventToState ");
    switch (event) {
      case NumberEvent.addEvent:
        yield state + 5;
        log("NumberBloc mapEventToState 更新状态");
        break;
    }
  }

  @override
  void onTransition(Transition<NumberEvent, int> transition) {
    super.onTransition(transition);
    log("NumberBloc onTransition ${transition}");
  }

  @override
  void onError(Object error, StackTrace stacktrace) {
    super.onError(error, stacktrace);
    log("NumberBloc onError ${error} , ${stacktrace}");
  }
}

/// BlocDelegate
// BlocSupervisor是一个单例，负责所有Bloc 并将职责委托给BlocDelegate.
// Bloc 2.1.1
// class SimpleBlocDelegate extends BlocDelegate {
//   @override
//   void onEvent(Bloc bloc, Object event) {
//     super.onEvent(bloc, event);
//     log("SimpleBlocDelegate onEvent");
//   }
//
//   @override
//   void onTransition(Bloc bloc, Transition transition) {
//     super.onTransition(bloc, transition);
//     log("SimpleBlocDelegate onTransition :  $transition");
//   }
//
//   @override
//   void onError(Bloc bloc, Object error, StackTrace stacktrace) {
//     super.onError(bloc, error, stacktrace);
//     log("SimpleBlocDelegate onError : $error");
//   }
// }
